#include "../include/head.h"

int set_arg(char *buf, char *arg) {
    // 读取到了conf文件里面完整的一行，进行提取处理
    char *p = strchr(buf, '=');
    if (p == NULL) {
        fprintf(stderr, "client.conf set wrong!\n");
        exit(-1);
    }
    
    // 将等号向后偏移两个字节的位置放入ip或者port
    strcpy(arg, p + 2);
    return 0;
}

int conf_get(char *conf, char *ip, char *port) {
    // 打开client.conf文件并读取内容
    FILE *fp = fopen(conf, "r");
    ERROR_CHECK(fp, NULL, "fopen");
    // 搞一个缓冲区，从conf中读取内容到缓冲区
    char buf[128] = {0};

    fgets(buf, sizeof(buf), fp);
    buf[strlen(buf) - 1] = '\0';
    set_arg(buf, ip);

    bzero(buf, sizeof(buf));
    fgets(buf, sizeof(buf), fp);
    buf[strlen(buf) - 1] = '\0';
    set_arg(buf, port);

    fclose(fp);
    return 0;


}
